<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Db;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Services\UCenterService;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    // public function showRegistrati(Request $request){
    //   dd($request);
    // }
    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            // 'qq' => 'required|string|max:255',
            'telphone' => 'required|digits:11',
            'password' => 'required|string|min:6|confirmed',
            'captcha' => 'required|captcha',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        $client = new UCenterService();
   		  $datas=array(
         'username'=>$data['name'],
     		 'password'=>MD5($data['password']),
   			 'repass'=>MD5($data['password_confirmation']),
   			 'regtime'=> time(),
   			 'cellphone'=>$data['telphone'],
   			 'email'=>$data['email'],
   			 'uptime'=>time(),
   			 'source'=>'9'
   	     );
   	   $re = $client->sys_regist($datas);
       if($re == -1) {
          // abort(403, '用户名已存在！');
          echo '用户名已存在!';
          exit;
       }else{
           $data['sysid']=$re;
           $data['password']=bcrypt($data['password']);
        return User::create($data);
       }
    }
}
